<template>
	<view>
		<radio-group @change="radioChange">
			<view class="item" v-for="(res, index) in addressList.list" :key="res.id">
				<radio :value="JSON.stringify(res)" :checked="res.addressId == store.flag"></radio>
				<view style="flex-grow: 1;">
					<view class="top">
						<view class="name">{{ res.username }}</view>
						<view class="phone">{{ res.phone }}</view>
						<view class="tag">
							<text v-if="res.status == '1'" style="background-color: #f3af28;">默认</text>
						</view>
					</view>
					<view class="bottom">
						{{res.area}},{{res.address}}
						<u-icon name="edit-pen" :size="40" color="#999999" @click="editAddress(res)"></u-icon>
					</view>
				</view>
			</view>
		</radio-group>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue'
	import {
		getAddressListApi
	} from '../../api/user';
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app';
	import { addressStore } from '../../store/address';
	const store = addressStore()
	//地址数据
	const addressList = reactive({
		list: []
	})
	//查询地址数据
	const getList = async () => {
		let result = await getAddressListApi({
			openId: uni.getStorageSync('openid')
		})
		if (result && result.code == 200) {
			addressList.list = result.data
		}
	}
	//跳转到新增地址
	const toAddSite = () => {
		uni.navigateTo({
			url: '../address/address'
		});
	}
	//修改地址
	const editAddress = (item) => {
		uni.navigateTo({
			url: '../address/address?item=' + JSON.stringify(item)
		});
	}
	//选择地址
	const radioChange = (e)=>{
		if(e.detail.value){
			let item = JSON.parse(e.detail.value)
			store.flag = item.addressId
			store.username = item.username
			store.phone = item.phone
			store.area = item.area
			store.address = item.address
			//退回到订单列表
			uni.navigateBack()
		}
	}
	onShow(() => {
		getList()
	})
</script>

<style lang="scss" scoped>
	.item {
		padding: 40rpx 20rpx;
		display: flex;
		align-items: center;

		.top {
			display: flex;
			font-weight: bold;
			font-size: 34rpx;

			.phone {
				margin-left: 60rpx;
			}

			.tag {
				display: flex;
				font-weight: normal;
				align-items: center;

				text {
					display: block;
					width: 60rpx;
					height: 34rpx;
					line-height: 34rpx;
					color: #ffffff;
					font-size: 20rpx;
					border-radius: 6rpx;
					text-align: center;
					margin-left: 30rpx;
					background-color: rgb(49, 145, 253);
				}

				.red {
					background-color: red
				}
			}
		}

		.bottom {
			display: flex;
			margin-top: 20rpx;
			font-size: 28rpx;
			justify-content: space-between;
			color: #999999;
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 600rpx;
		line-height: 100rpx;
		position: absolute;
		bottom: 30rpx;
		left: 80rpx;
		background-color: #f3af28;
		border-radius: 60rpx;
		font-size: 30rpx;

		.add {
			display: flex;
			align-items: center;
			color: #ffffff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>